/*
 *  $Id$
 *  This file is a part of Ragima CCG.
 *  (C) 2008-2009 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima.Cards001.Air.Creatures {
    public class UniqueArgus : CreatureCard {
        public UniqueArgus() : base("001_a_c_u_argus") {
            Level = CompleteLevel.ReadyForTest;
            Version = 0;
            Element = ElementType.Air;
            Value = 100;
            MaxCount = 1;
            Damage = 3;
            Health = 8;
            AddManaCost(10);
        }

        public override void SetNaturalAffects(ICollector<Affect> affects) {
            affects.Add<Affects.Hero>();
            affects.Add<Affects.Tireless>();
            affects.Add<Affects.Antimagic>();
            affects.Add<ArgusAffect>();
        }

        private class ArgusAffect : Affect {
            private List<AbilityCard> _abilities;

            public ArgusAffect() {
                _abilities = new List<AbilityCard>();
            }

            public override string Id {
                get { return "argus"; }
            }

            public override bool Dispellable {
                get { return base.Dispellable; }
            }

            public override void SetAffect() {
                base.SetAffect();
                foreach (AbilityCard ability in _abilities)
                    LocalSummary.AddAbility(ability);
                GlobalSummary.OnUseCard += DoCard;
                EnemySummary.OnUseCard += DoCard;
                GlobalSummary.OnUseAbility += DoAbility;
                EnemySummary.OnUseAbility += DoAbility;
                LocalSummary.OnAfterDamage += DoDamage;
            }

            public override void ResetAffect() {
                base.ResetAffect();
                foreach (AbilityCard ability in _abilities)
                    LocalSummary.RemoveAbility(ability);
                LocalSummary.OnUseAbility -= DoAbility;
                LocalSummary.OnAfterDamage -= DoDamage;
            }

            private void AddAbility(AbilityCard ability) {
                if (_abilities.Contains(ability))
                    return;
                _abilities.Add(ability);
                LocalSummary.AddAbility(ability);
            }
            private void RemoveAbility(AbilityCard ability) {
                if (_abilities.Remove(ability))
                    LocalSummary.RemoveAbility(ability);
            }

            private void DoCard(Card card, TargetArgs args) {
                Creature argus = Owner as Creature;
                SpellCard spell = card as SpellCard;
                TargetArgs<Creature> ca = args as TargetArgs<Creature>;
                if (argus == null || spell == null || ca == null)
                    return;
                if (argus.DistanceTo(ca.Target) < 2)
                    AddAbility(new SpellAbilityCard(spell));
            }
            private void DoAbility(AbilityCard ability, TargetArgs args) {
                Creature argus = Owner as Creature;
                Creature caster = args.Caster as Creature;
                if (argus == null || caster == null)
                    return;
                if (argus == caster) {
                    RemoveAbility(ability);
                    return;
                }
                if (argus.DistanceTo(caster) < 2)
                    AddAbility(ability);
            }

            private void DoDamage(AbstractObject target, DamageInfo damage) {
                if (damage.HealthChange > 0 && damage.DmgType == DamageType.PhysicalAttack) {
                    int value = 0;
                    Affects.Wound wound = target.GetAffect<Affects.Wound>();
                    if (wound != null) {
                        value = wound.Value;
                        target.RemoveAffect(wound);
                    }
                    target.AddAffect(new Affects.Wound(value + 1));
                }
            }
        }
    }
}
